package cn.stylefeng.guns.modular.recy.utils;

import com.diboot.core.entity.Dictionary;
import com.diboot.core.service.*;
import com.diboot.core.util.*;
import java.util.*;
import java.lang.annotation.*;
import com.diboot.core.binding.annotation.*;
import com.diboot.core.binding.binder.*;
import com.diboot.core.binding.parser.*;
import com.baomidou.mybatisplus.extension.service.*;
import com.diboot.core.entity.*;
import org.slf4j.*;

public class MyRelationsBinder
{
    private static final Logger log;
    
    public static <E, VO> List<VO> convertAndBind(final List<E> entityList, final Class<VO> voClass) {
        final List<VO> voList = (List<VO>)EntityBind.convertList((List)entityList, (Class)voClass);
        bind(voList);
        return voList;
    }
    
    public static <VO> void bind(final List<VO> voList) {
        if (!V.isEmpty((Collection)voList)) {
            final Class voClass = voList.get(0).getClass();
            final BindAnnotationGroup bindAnnotationGroup = BindAnnotationGroupCache.getBindAnnotationGroup(voClass);
            if (bindAnnotationGroup.isNotEmpty()) {
                final List<FieldAnnotation> dictAnnoList = (List<FieldAnnotation>)bindAnnotationGroup.getBindDictAnnotations();
                if (dictAnnoList != null) {
                    for (final FieldAnnotation annotation : dictAnnoList) {
                        doBindingDict(voList, annotation);
                    }
                }
                final List<FieldAnnotation> fieldAnnoList = (List<FieldAnnotation>)bindAnnotationGroup.getBindFieldAnnotations();
                if (fieldAnnoList != null) {
                    doBindingField(voList, fieldAnnoList);
                }
                final List<FieldAnnotation> entityAnnoList = (List<FieldAnnotation>)bindAnnotationGroup.getBindEntityAnnotations();
                if (entityAnnoList != null) {
                    for (final FieldAnnotation anno : entityAnnoList) {
                        doBindingEntity(voList, anno);
                    }
                }
                final List<FieldAnnotation> entitiesAnnoList = (List<FieldAnnotation>)bindAnnotationGroup.getBindEntityListAnnotations();
                if (entitiesAnnoList != null) {
                    for (final FieldAnnotation anno2 : entitiesAnnoList) {
                        doBindingEntityList(voList, anno2);
                    }
                }
            }
        }
    }
    
    private static <VO> void doBindingDict(final List<VO> voList, final FieldAnnotation fieldAnno) {
        final DictionaryService dictionaryService = (DictionaryService)ContextHelper.getBean((Class)DictionaryService.class);
        if (dictionaryService != null) {
            final BindDict annotation = (BindDict)fieldAnno.getAnnotation();
            dictionaryService.bindItemLabel((List)voList, fieldAnno.getFieldName(), annotation.field(), annotation.type());
        }
    }
    
    private static <VO> void doBindingField(final List<VO> voList, final List<FieldAnnotation> fieldAnnoList) {
        final Map<String, List<FieldAnnotation>> clazzToListMap = new HashMap<String, List<FieldAnnotation>>();
        for (final FieldAnnotation anno : fieldAnnoList) {
            final BindField bindField = (BindField)anno.getAnnotation();
            final String key = bindField.entity().getName() + ":" + bindField.condition();
            final List<FieldAnnotation> list = clazzToListMap.computeIfAbsent(key, k -> new ArrayList());
            list.add(anno);
        }
        for (final Map.Entry<String, List<FieldAnnotation>> entry : clazzToListMap.entrySet()) {
            final List<FieldAnnotation> list2 = entry.getValue();
            final BindField bindAnnotation = (BindField)list2.get(0).getAnnotation();
            final FieldBinder binder = buildFieldBinder((Annotation)bindAnnotation, voList);
            for (final FieldAnnotation anno2 : list2) {
                final BindField bindField2 = (BindField)anno2.getAnnotation();
                binder.link(bindField2.field(), anno2.getFieldName());
            }
            parseConditionsAndBinding((BaseBinder)binder, bindAnnotation.condition());
        }
    }
    
    private static <VO> void doBindingEntity(final List<VO> voList, final FieldAnnotation fieldAnnotation) {
        final BindEntity annotation = (BindEntity)fieldAnnotation.getAnnotation();
        final EntityBinder binder = buildEntityBinder((Annotation)annotation, voList);
        if (binder != null) {
            binder.set(fieldAnnotation.getFieldName());
            parseConditionsAndBinding((BaseBinder)binder, annotation.condition());
        }
    }
    
    private static <VO> void doBindingEntityList(final List<VO> voList, final FieldAnnotation fieldAnnotation) {
        final BindEntityList bindAnnotation = (BindEntityList)fieldAnnotation.getAnnotation();
        final EntityListBinder binder = buildEntityListBinder((Annotation)bindAnnotation, voList);
        if (binder != null) {
            binder.set(fieldAnnotation.getFieldName());
            parseConditionsAndBinding((BaseBinder)binder, bindAnnotation.condition());
        }
    }
    
    private static void parseConditionsAndBinding(final BaseBinder binder, final String condition) {
        try {
            ConditionManager.parseConditions(condition, binder);
            binder.bind();
        }
        catch (Exception var3) {
            MyRelationsBinder.log.error("\u89e3\u6790\u6ce8\u89e3\u6761\u4ef6\u4e0e\u7ed1\u5b9a\u6267\u884c\u5f02\u5e38", (Throwable)var3);
        }
    }
    
    private static FieldBinder buildFieldBinder(final Annotation annotation, final List voList) {
        final IService service = getService(annotation);
        return (service != null) ? new FieldBinder(service, voList) : null;
    }
    
    private static EntityBinder buildEntityBinder(final Annotation annotation, final List voList) {
        final IService service = getService(annotation);
        return (service != null) ? new EntityBinder(service, voList) : null;
    }
    
    private static EntityListBinder buildEntityListBinder(final Annotation annotation, final List voList) {
        final IService service = getService(annotation);
        return (service != null) ? new EntityListBinder(service, voList) : null;
    }
    
    private static IService getService(final Annotation annotation) {
        Class<?> entityClass = null;
        if (annotation instanceof BindDict) {
            entityClass = Dictionary.class;
        }
        else if (annotation instanceof BindField) {
            final BindField bindAnnotation = (BindField)annotation;
            entityClass = (Class<?>)bindAnnotation.entity();
        }
        else if (annotation instanceof BindEntity) {
            final BindEntity bindAnnotation2 = (BindEntity)annotation;
            entityClass = (Class<?>)bindAnnotation2.entity();
        }
        else {
            if (!(annotation instanceof BindEntityList)) {
                MyRelationsBinder.log.warn("\u975e\u9884\u671f\u7684\u6ce8\u89e3: " + annotation.getClass().getSimpleName());
                return null;
            }
            final BindEntityList bindAnnotation3 = (BindEntityList)annotation;
            entityClass = (Class<?>)bindAnnotation3.entity();
        }
        final IService service = ContextHelper.getServiceByEntity(entityClass);
        if (service == null) {
            MyRelationsBinder.log.error("\u672a\u80fd\u8bc6\u522b\u5230Entity: " + entityClass.getName() + " \u7684Service\u5b9e\u73b0\uff01");
        }
        return service;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)MyRelationsBinder.class);
    }
}
